<?php

namespace App\Http\Controllers;

use App\Models\Admin;
use App\Models\Child;
use App\Models\Device;
use App\Models\DeviceGrade;
use App\Models\Game;
use App\Models\History;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Storage;
use Tymon\JWTAuth\Facades\JWTAuth;
//use Maatwebsite\Excel\Excel;
use Maatwebsite\Excel\Facades\Excel;

class TestController extends Controller
{
    //

    public function index(){
       // $deviceGrade = DeviceGrade::where("device_id", 1)->pluck("grade_id")->toArray();
        //dump($deviceGrade);
        /*
        $children = Child::get();

        $admin = Admin::find(1);
        //echo decrypt($admin->password);
        echo $admin->password;
        echo '<br />';
        $s = '123456';
        echo bcrypt($s);

        return Hash::check($s, $admin->password) ? 'yes' : 'no';
        */

/*
 //修改学生名字为随机汉字
        $children->each(function(&$item){
           $item->update(['name'=>$this->getChar()]);
        });*/
        /*
       // header( "Content-type: image/jpeg");
        $path = Storage::disk(env("FILESYSTEM_DRIVER"))->path('images/2068764.jpeg');
        $PSize = filesize($path);
        $picturedata = fread(fopen($path, "r"), $PSize);

        echo response()->json(['status'=>200, 'image'=>base64_encode($picturedata)]);
        */
$num = [7,13];
$rand = rand($num[0],$num[1]);
dump($rand);
    }
    private function getChar( $num = 3 )  // $num为生成汉字的数量

    {

        $b = '';

        for ($i=0; $i<$num; $i++) {

            // 使用chr()函数拼接双字节汉字，前一个chr()为高位字节，后一个为低位字节

            $a = chr(mt_rand(0xB0,0xD0)).chr(mt_rand(0xA1, 0xF0));

            // 转码

            $b .= iconv('GB2312', 'UTF-8', $a);

        }

        return $b;
    }

    function test(){

        $child_num = 5;
        $basetime = "2019-08-01";
        $t = strtotime($basetime);
        $testdatatime = 40;
        $brange = [5,25];
        $trange = [25,60];
        $wrange = [30, 80];

        $btime = 5;
        $ttime = 25;
        //$wtime = 10;
        $result = [];

        $d = 3600*24;


        for ($i=0; $i<$child_num; $i++){
            $tmp['child_num'] = $i+1;
            $eye = $face = $hand = 0;

            for ($j = 0; $j<$testdatatime; $j++){
                $tmp['testdate'] = date('Y-m-d',$t + $d*$j + rand(25200,39600));
                $tmp['datetime'] = $j+1;

                if ($j <= $btime){
                    $eye = rand($brange[0],$brange[1]);
                    $face =  rand($brange[0],$brange[1]);
                    $hand = rand($brange[0],$brange[1]);

                    $tmp['step_id'] = 1;
                    $tmp['step'] = "基线期";
                    $tmp['eye'] = $eye;
                    $tmp['face'] = $face;
                    $tmp['hand'] = $hand;

                }elseif ($j<=$ttime){
                    $eye = $eye + rand(-2,5);
                    $face = $face + rand(-2,4);
                    $hand = $hand + rand(-2,4);
                    $tmp['step_id'] = 2;
                    $tmp['step'] = "干预期";
                    $tmp['eye'] = $eye;
                    $tmp['face'] = $face;
                    $tmp['hand'] = $hand;
                }else{
                    $eye = $eye + rand(-2,2);
                    $face = $face + rand(-2,2);
                    $hand = $hand + rand(-2,2);

                    $tmp['step_id'] = 3;
                    $tmp['step'] = "维持期";
                    $tmp['eye'] = $eye;
                    $tmp['face'] = $face;
                    $tmp['hand'] = $hand;
                }
                $result[] = $tmp;
            }
        }
        //dump($result);
//Excel文件导出功能 By Laravel学院

            $cellData = [
                ['学号','姓名','成绩'],
                ['10001','AAAAA','99'],
                ['10002','BBBBB','92'],
                ['10003','CCCCC','95'],
                ['10004','DDDDD','89'],
                ['10005','EEEEE','96'],
            ];
            /*
            Excel::create('学生成绩',function($excel) use ($cellData){
                $excel->sheet('score', function($sheet) use ($cellData){
                    $sheet->rows($cellData);
                });
            });
            */
/*
        Excel::download/Excel::store('学生成绩',function($excel) use ($cellData){
                $excel->sheet('score', function($sheet) use ($cellData){
                    $sheet->rows($cellData);
                });
            });*/
        $filePath = 'test.xlsx';

        collect($result)->downloadExcel(
            $filePath,
            $writerType = null,
            $headings = false
        );


    }
}
